package source.view;

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;

/**
 * The Key and Mouse Listener fo the Key Configuration Dialog box.  This box
 * will tell the JDialog to fire approriate PropertyChangeEvents to the 
 * PlayerKeyListener.
 * @author Steve
 *
 */
public class KeyMapDialogListener implements KeyListener, MouseListener{
	private JComponent comp;
	private boolean selected;
	private JDialog dialog;
	
	public KeyMapDialogListener(JDialog dialog){
		this.comp = new JLabel();
		selected = false;
		this.dialog = dialog;
	}

	public void keyPressed(KeyEvent e) {
		if(selected){
			if(((KeyMapDialog)dialog).requestKeyChange(((KeyMapLabel)comp).getID(), e.getKeyCode()))
			{
				String text = KeyEvent.getKeyText(e.getKeyCode());
				((JLabel)comp).setText(text);
				comp.setOpaque(false);
				selected = false;
			}
		}
	}

	public void keyReleased(KeyEvent e) {}

	public void keyTyped(KeyEvent e) {}

	public void mouseClicked(MouseEvent e) {}

	public void mouseEntered(MouseEvent e) {}

	public void mouseExited(MouseEvent e) {}

	public void mousePressed(MouseEvent e) {
		JComponent oldComp = comp;
		comp = (JComponent) e.getSource();

		
		if(selected){
			oldComp.setOpaque(false);
			comp.setOpaque(true);
			comp.setBackground(Color.RED);
			
		}
		else{
			comp.setOpaque(true);
			comp.setBackground(Color.RED);
			selected = !selected;
		}
		dialog.repaint();
	}

	public void mouseReleased(MouseEvent e) {
		if(comp.getName().equals("submit")){
			dialog.dispose();
		}
	}
	
}